﻿using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using System.Data.OleDb;

namespace Web.Pages.QuShui.DiXiaS.ShouFeiGL
{
    public partial class HuiGuanTF : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtGrid
        {
            get
            {
                return ViewState["dtGrid"] as DataTable;
            }
            set
            {
                ViewState["dtGrid"] = value;
            }
        }
      
        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        
        /// <summary>
        /// 管理部门
        /// </summary>
        QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();

        /// <summary>
        /// 回灌退费
        /// </summary>
        QS_SF_HUIGUANTF HGTF = new QS_SF_HUIGUANTF();

        /// <summary>
        /// 回灌退费BLL
        /// </summary>
        QS_SF_HUIGUANTFBLL HGTFBLL = new QS_SF_HUIGUANTFBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
                LoadGridData();
            }
            ScriptManager1.RegisterPostBackControl(btnExport);
            gvList.DataSource = dtGrid;
            gvList.DataBind();

        }

        /// <summary>
        /// 查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            LoadGridData();
        }

        /// <summary>
        /// tab1导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string sName = this.cmbYear.Text + "回灌退费" + ".xls";
            if (this.dtGrid.Rows.Count > 0)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("管理单位");
                dt.Columns.Add("回灌号");
                dt.Columns.Add("用户名");
                dt.Columns.Add("回灌井");
                dt.Columns.Add("井地址");
                dt.Columns.Add("回灌计划(万立方米)");
                dt.Columns.Add("回灌单价(元)");
                dt.Columns.Add("回灌量(立方米)");
                dt.Columns.Add("未退回灌水费(万元)");
                dt.Columns.Add("已退回灌水费(万元)");
                dt.Columns.Add("预退回灌水费(万元)");
                dt.Columns.Add("年回灌能力(万立方米/年)");
                dt.Columns.Add("现回灌能力(立方米/小时)");
                dt.Columns.Add("测试回灌能力(立方米/小时)");
                for (int i = 0; i < this.gvList.VisibleRowCount; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["管理单位"] = this.gvList.GetRowValues(i, "S_BUMENMC");
                    dr["回灌号"] = this.gvList.GetRowValues(i, "S_YONGHUH");
                    dr["用户名"] = this.gvList.GetRowValues(i, "S_YONGHUM");
                    dr["回灌井"] = this.gvList.GetRowValues(i, "S_JINGBH");
                    dr["井地址"] = this.gvList.GetRowValues(i, "S_JINGDIZ");
                    dr["回灌计划(万立方米)"] = this.gvList.GetRowValues(i, "N_HUIGUANJH");
                    dr["回灌单价(元)"] = this.gvList.GetRowValues(i, "N_HUIGUANDJ");
                    dr["回灌量(立方米)"] = this.gvList.GetRowValues(i, "I_HUIGUANL");
                    dr["未退回灌水费(万元)"] = this.gvList.GetRowValues(i, "N_TUIFEI_W");
                    dr["已退回灌水费(万元)"] = this.gvList.GetRowValues(i, "N_TUIFEI_Y");
                    dr["预退回灌水费(万元)"] = this.gvList.GetRowValues(i, "N_TUIFEI_Z");
                    dr["年回灌能力(万立方米/年)"] = this.gvList.GetRowValues(i, "N_NHGNL");
                    dr["现回灌能力(立方米/小时)"] = this.gvList.GetRowValues(i, "N_XHGNL");
                    dr["测试回灌能力(立方米/小时)"] = this.gvList.GetRowValues(i, "N_CSHGNL");
                    dt.Rows.Add(dr);
                }
                PageTools.CreateExcel(dt, sName);
                //PageTools.CreateExcel(this.dtGrid, sName);
                //PageTools.CreateExcel2();

            }
        }


        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            int wtype = BusinessTool.WType;
            //gvList.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            int water_y_m = Convert.ToInt32(BusinessTool.GetSLYMorZWNY(1, wtype));
            for (int i = DateTime.Now.Year - 10; i <= DateTime.Now.Year; i++)
            {
                cmbYear.Items.Add(i.ToString(), i.ToString());
            }
            cmbYear.Value = water_y_m / 100;
            //for (int i = 1; i <= 12; i++)
            //{
            //    cmbMonth.Items.Add(i.ToString(), i);
            //}
            //cmbMonth.Value = water_y_m % 100;
            LoadGLBM();
            int userid = LoginInfo.CurrentUser.UserID;
            if (userid == 111 || userid == 116 || userid == 99999 || userid == 117)
            {
                btnSave.Visible = true;
                //btnCancel.Visible = true;
            }

        }



        /// <summary>
        /// 加载管理部门
        /// </summary>
        private void LoadGLBM()
        {
            QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();
            DataTable dt = GLBM.SelectQS_JX_BUMENXXesDynamic("I_JLZT=0 AND S_BUMENMC IS NOT NULL", null);
            cobGLDW.Items.Insert(0, new ListEditItem("", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cobGLDW.Items.Add(dt.Rows[i]["S_BUMENMC"].ToString(), dt.Rows[i]["S_BUMENBH"]);
            }
           
        }

        /// <summary>
        /// 加载查询数据
        /// </summary>
        private void LoadGridData()
        {
            try
            {
                int y = Convert.ToInt32(cmbYear.Value);
                DataTable dt1 = _SpHelper.P_DXSSF_HGTF(y, 0,0).Tables[0];
                dtGrid = dt1;
                gvList.DataSource = dtGrid;
                gvList.DataBind();
                zjl.Text = "共" + gvList.VisibleRowCount + "条";
                gvList.FocusedRowIndex = -1;
                gvList.FocusedRowIndex = 0;
                int  hj2 = 0, hj3 = 0;
                decimal hj1 = 0, hj4 = 0, hj5 = 0, hj6 = 0;
                for (int i = 0; i < dtGrid.Rows.Count; i++)
                {
                    hj1 += dtGrid.Rows[i]["N_NHGNL"].ToString() != "" ? Convert.ToDecimal(dtGrid.Rows[i]["N_NHGNL"]) : 0;
                    hj2 += dtGrid.Rows[i]["N_HUIGUANJH"].ToString() != "" ? Convert.ToInt32(dtGrid.Rows[i]["N_HUIGUANJH"]) : 0;
                    hj3 += dtGrid.Rows[i]["I_HUIGUANL"].ToString() != "" ? Convert.ToInt32(dtGrid.Rows[i]["I_HUIGUANL"]) : 0;
                    hj4 += dtGrid.Rows[i]["N_TUIFEI_W"].ToString() != "" ? Convert.ToDecimal(dtGrid.Rows[i]["N_TUIFEI_W"]) : 0;
                    hj5 += dtGrid.Rows[i]["N_TUIFEI_Y"].ToString() != "" ? Convert.ToDecimal(dtGrid.Rows[i]["N_TUIFEI_Y"]) : 0;
                    hj6 += dtGrid.Rows[i]["N_TUIFEI_Z"].ToString() != "" ? Convert.ToDecimal(dtGrid.Rows[i]["N_TUIFEI_Z"]) : 0;
                }
                txthe1.Text = hj1.ToString();
                txthe11.Text = hj2.ToString();
                txthe2.Text = hj3.ToString();
                txthe3.Text = hj4.ToString();
                txthe4.Text = hj5.ToString();
                txthe5.Text = hj6.ToString();

            }
            catch (Exception ex)
            {

            }
        }

       

        /// <summary>
        /// 前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            string msg = "";
            if (e.Parameter == "setinfo")
            {
               
                    int y = Convert.ToInt32(cmbYear.Value);

                    DataTable dt_temp = _SpHelper.P_DXSSF_HGTF(y, id,0).Tables[0];
                    if (dt_temp != null)
                    {
                    msg = string.Join("|",
                        dt_temp.Rows[0]["S_BUMENMC"].ToString(),
                        dt_temp.Rows[0]["S_YONGHUH"].ToString(),
                        dt_temp.Rows[0]["S_JINGBH"].ToString(),
                        dt_temp.Rows[0]["N_NHGNL"].ToString(),
                        dt_temp.Rows[0]["N_XHGNL"].ToString(),
                        dt_temp.Rows[0]["N_CSHGNL"].ToString(),
                        dt_temp.Rows[0]["I_HUIGUANL"].ToString(),
                        dt_temp.Rows[0]["N_HUIGUANJH"].ToString(),
                        dt_temp.Rows[0]["N_HUIGUANDJ"].ToString(),
                        dt_temp.Rows[0]["N_TUIFEI_W"].ToString(),
                        dt_temp.Rows[0]["N_TUIFEI_Y"].ToString(),
                        dt_temp.Rows[0]["N_TUIFEI_Z"].ToString()
                        
                        );
                }
            }

            else if (e.Parameter == "save")
            {
                
                OperateState operateState;

                //添加
                if (id == -1)
                {
                    this.HGTF.ID = 0;
                    operateState = OperateState.Add;
                }
                //修改
                else
                {
                    //修改操作
                    this.HGTF = this.HGTFBLL.SelectQS_SF_HUIGUANTF(id);
                    operateState = OperateState.Modify;
                }
                if (cobGLDW.Text != "")
                {
                    HGTF.S_BUMENBH = cobGLDW.Value != Convert.DBNull ? this.cobGLDW.Value.ToString() : "";
                    HGTF.S_BUMENMC = this.cobGLDW.Text != "" ? this.cobGLDW.Text : "";
                }
                HGTF.S_YONGHUH = txtHGH.Text != "" ? txtHGH.Text : null;
                //HGTF.S_YONGHUM = "";
                HGTF.S_JINGBH = txtHGJ.Text;
                //decimal? nhgnl = txtHGNL.Text != "" ? (int?)Convert.ToDecimal(txtHGNL.Text) : null;
                //HGTF.N_NHGNL = nhgnl;
                //decimal? XHGNL = txtXHGNL.Text != "" ? (decimal?)Convert.ToDecimal(txtXHGNL.Text) : null;
                //HGTF.N_XHGNL = XHGNL;
                decimal? hgjh = txtHGJH.Text != "" ? (int?)Convert.ToDecimal(txtHGJH.Text) : null;
                HGTF.N_HUIGUANJH = hgjh;
                decimal? hgdj = txtHGDW.Text != "" ? (decimal?)Convert.ToDecimal(txtHGDW.Text) : null;
                HGTF.N_HUIGUANDJ = hgdj;
                int? hgl = txtHGL.Text != "" ? (int?)Convert.ToInt32(txtHGL.Text) : null;
                HGTF.I_HUIGUANL = hgl;
                decimal? TUIFEI_W = txtWTHGF.Text != "" ? (decimal?)Convert.ToDecimal(txtWTHGF.Text) : null;
                HGTF.N_TUIFEI_W = TUIFEI_W;
                decimal? TUIFEI_y = txtYTHGF2.Text != "" ? (decimal?)Convert.ToDecimal(txtYTHGF2.Text) : null;
                HGTF.N_TUIFEI_Y = TUIFEI_y;
                decimal? TUIFEI_z = txtYTHGF.Text != "" ? (decimal?)Convert.ToDecimal(txtYTHGF.Text) : null;
                HGTF.N_TUIFEI_Z = TUIFEI_z;
                decimal? chHGNL = txtCHHGNL.Text != "" ? (decimal?)Convert.ToDecimal(txtCHHGNL.Text) : null;
                HGTF.N_CSHGNL = chHGNL;
                HGTF.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                HGTF.D_CAOZUORQ = DateTime.Now;



                //设置操作信息
                //LoginInfo.CurrentUser.UserID.ToString();
                 //DateTime.Now;
                _SYS_LOGBLL.Log<QS_SF_HUIGUANTF>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "回灌退费", operateState, HGTF, null);
                if (HGTFBLL.InsertOrUpdate(HGTF, null))
                {
                   
                    msg = string.Join("|", "1");
                    LoadGridData();
                }
            }
            e.Result = e.Parameter + "|0|" + msg;
        }

        protected void gvList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadGridData();
        }
       
        #endregion

        protected void btnsx_Click(object sender, EventArgs e)
        {
            int y = Convert.ToInt32(cmbYear.Value);
            DataTable dt1 = _SpHelper.P_DXSSF_HGTF(y, 0, 1).Tables[0];
            if (dt1.Rows.Count>0)
            {
                LoadGridData();
            }
            
        }

    }
}